//
//  MHRateView.m
//  MHO
//
//  Created by papo on 4/2/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHRateView.h"

@implementation MHRateView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

+ (MHRateView*)rateViewFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:@"MHRateView" owner:self options:nil] objectAtIndex:0];
}

- (void)setRateFloat:(CGFloat)rate {
    CGFloat fiveRate = floorf(5.0 * rate);
    NSInteger count = (NSInteger)fiveRate;
    for (NSInteger i = 1; i <= 5; i++) {
        UIImageView* star = (UIImageView*)[self viewWithTag:i];
        if (star && [star isKindOfClass:[UIImageView class]]) {
            if (i <= count) {
                [star setImage:[UIImage imageNamed:@"star_yellow_small.png"]];
            }
            else if ((i == count + 1) && (rate >= fiveRate + 0.5)) {
                [star setImage:[UIImage imageNamed:@"star_gray_small_50.png"]];
            }
            else {
                [star setImage:[UIImage imageNamed:@"star_gray_small.png"]];
            }
        }
    }
}

- (void)setRateInt:(NSInteger)rate {
    [self setRateFloat:(CGFloat)rate / 100.0];
}

@end
